<?php

/**
 * Webman gRPC扩展包性能基准测试
 * 
 * 这个脚本用于测试扩展包的性能，包括连接池、序列化优化和内存管理
 */

require_once __DIR__ . '/vendor/autoload.php';

use WebmanGrpc\ConnectionPool;
use WebmanGrpc\SerializationOptimizer;
use WebmanGrpc\MemoryManager;
use WebmanGrpc\PerformanceMonitor;
use WebmanGrpc\GrpcManager;

echo "Webman gRPC扩展包性能基准测试\n";
echo "==============================\n\n";

// 初始化组件
GrpcManager::getInstance()->initialize();

// 测试连接池性能
echo "1. 连接池性能测试\n";
echo "-----------------\n";

$connectionPool = ConnectionPool::getInstance();
$startTime = microtime(true);

// 模拟获取100个连接
for ($i = 0; $i < 100; $i++) {
    $connection = $connectionPool->getConnection('test', [
        'host' => 'localhost',
        'port' => 50051
    ]);
}

$endTime = microtime(true);
$duration = $endTime - $startTime;

echo "获取100个连接耗时: " . round($duration * 1000, 2) . " ms\n";
echo "平均每个连接耗时: " . round(($duration * 1000) / 100, 2) . " ms\n";

$stats = $connectionPool->getStats();
echo "连接池统计信息: " . json_encode($stats, JSON_PRETTY_PRINT) . "\n\n";

// 测试序列化优化性能
echo "2. 序列化优化性能测试\n";
echo "---------------------\n";

$serializationOptimizer = SerializationOptimizer::getInstance();
$startTime = microtime(true);

// 模拟序列化1000个对象
for ($i = 0; $i < 1000; $i++) {
    $data = ['id' => $i, 'name' => 'test_' . $i, 'data' => str_repeat('x', 100)];
    $serialized = $serializationOptimizer->serialize($data);
    $unserialized = $serializationOptimizer->unserialize($serialized);
}

$endTime = microtime(true);
$duration = $endTime - $startTime;

echo "序列化/反序列化1000个对象耗时: " . round($duration * 1000, 2) . " ms\n";
echo "平均每个对象耗时: " . round(($duration * 1000) / 1000, 2) . " ms\n";

$stats = $serializationOptimizer->getStats();
echo "序列化优化器统计信息: " . json_encode($stats, JSON_PRETTY_PRINT) . "\n\n";

// 测试内存管理性能
echo "3. 内存管理性能测试\n";
echo "-------------------\n";

$memoryManager = MemoryManager::getInstance();
$initialMemory = $memoryManager->getMemoryUsage();

// 模拟内存分配
$objects = [];
for ($i = 0; $i < 1000; $i++) {
    $objects[] = str_repeat('x', 1024); // 1KB per object
}

$peakMemory = $memoryManager->getMemoryUsage();
$memoryDiff = $peakMemory['current'] - $initialMemory['current'];

echo "分配1000个1KB对象后内存增加: " . round($memoryDiff / 1024 / 1024, 2) . " MB\n";

// 清理对象
unset($objects);
$memoryManager->checkMemoryUsage();

$finalMemory = $memoryManager->getMemoryUsage();
echo "清理后内存使用: " . round($finalMemory['current'] / 1024 / 1024, 2) . " MB\n";

$stats = $memoryManager->getStats();
echo "内存管理器统计信息: " . json_encode($stats, JSON_PRETTY_PRINT) . "\n\n";

// 测试性能监控
echo "4. 性能监控测试\n";
echo "---------------\n";

$performanceMonitor = PerformanceMonitor::getInstance();
$performanceMonitor->enableMonitoring();

// 模拟请求处理
for ($i = 0; $i < 100; $i++) {
    $requestId = $performanceMonitor->startRequest('test_service', 'test_method');
    
    // 模拟处理时间
    usleep(rand(1000, 5000)); // 1-5ms
    
    $performanceMonitor->endRequest($requestId);
}

$report = $performanceMonitor->generateReport();
echo "性能监控报告:\n";
echo json_encode($report, JSON_PRETTY_PRINT) . "\n\n";

// 综合性能测试
echo "5. 综合性能测试\n";
echo "---------------\n";

$startTime = microtime(true);

// 模拟综合场景
for ($i = 0; $i < 100; $i++) {
    // 获取连接
    $connection = $connectionPool->getConnection('test', [
        'host' => 'localhost',
        'port' => 50051
    ]);
    
    // 序列化数据
    $data = ['id' => $i, 'name' => 'test_' . $i, 'data' => str_repeat('x', 100)];
    $serialized = $serializationOptimizer->serialize($data);
    
    // 模拟请求处理
    $requestId = $performanceMonitor->startRequest('test_service', 'test_method');
    usleep(rand(1000, 5000)); // 1-5ms
    $performanceMonitor->endRequest($requestId);
    
    // 反序列化数据
    $unserialized = $serializationOptimizer->unserialize($serialized);
    
    // 检查内存使用
    if ($i % 20 == 0) {
        $memoryManager->checkMemoryUsage();
    }
}

$endTime = microtime(true);
$totalDuration = $endTime - $startTime;

echo "综合性能测试完成，总耗时: " . round($totalDuration * 1000, 2) . " ms\n";
echo "平均每次循环耗时: " . round(($totalDuration * 1000) / 100, 2) . " ms\n";

// 最终统计
echo "\n最终统计信息:\n";
echo "-------------\n";

echo "连接池统计: " . json_encode($connectionPool->getStats(), JSON_PRETTY_PRINT) . "\n";
echo "序列化优化器统计: " . json_encode($serializationOptimizer->getStats(), JSON_PRETTY_PRINT) . "\n";
echo "内存管理器统计: " . json_encode($memoryManager->getStats(), JSON_PRETTY_PRINT) . "\n";
echo "性能监控统计: " . json_encode($performanceMonitor->generateReport(), JSON_PRETTY_PRINT) . "\n";

echo "\n基准测试完成!\n";